package cn.calm.commons.service.defaultimpl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import cn.calm.commons.dao.QueryConditionBuilder;
import cn.calm.commons.entity.BaseEntity;

public class PropertyInQueryConditionBuilder<T extends BaseEntity> implements
		QueryConditionBuilder<T> {
	private String property;
	private Object[] value;

	public PropertyInQueryConditionBuilder(String property, Object[] value) {
		this.property = property;
		this.value = value;
	}

	@Override
	public void query(CriteriaBuilder r, CriteriaQuery<?> query,
			Root<? extends BaseEntity> root) {
		List<Predicate> list = new ArrayList<Predicate>();
		for (Object o : value) {
			list.add(r.equal(root.get(property), o));
		}
		Predicate[] opre = new Predicate[list.size()];
		list.toArray(opre);
		query.where(r.or(opre));
	}
}
